package gota.processingResources;

import java.io.File;
 
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import gate.Factory;
import gate.FeatureMap;
import gate.creole.SerialAnalyserController;

import org.w3c.dom.Node;

public class GazetterResource implements IProcessingResource {

	@Override
	public String getResourceName() {
		return "Gazetter";
	}

	@SuppressWarnings("null")
	@Override
	public void init(SerialAnalyserController controller, Node node)
			throws Exception {
		XPath xpath = XPathFactory.newInstance().newXPath();
		org.w3c.dom.Node listUrlNode = (org.w3c.dom.Node) xpath.evaluate(
				"//ListUrl", node, XPathConstants.NODE);
		org.w3c.dom.Node caseSensitivityNode = (org.w3c.dom.Node) xpath
				.evaluate("//CaseSensitive", node, XPathConstants.NODE);
                org.w3c.dom.Node encodingNode = (org.w3c.dom.Node) xpath
				.evaluate("//Encoding", node, XPathConstants.NODE);
                String encoding = "UTF-8";
                if(encodingNode ==null){
                    encoding = encodingNode.getTextContent();
                }
		String listUrl = listUrlNode.getTextContent();
		String caseSensitivity = caseSensitivityNode.getTextContent();
		FeatureMap params = Factory.newFeatureMap();
		params
				.put(
						gate.creole.gazetteer.DefaultGazetteer.DEF_GAZ_LISTS_URL_PARAMETER_NAME,
						new File(listUrl).toURI().toURL());
		params
				.put(
						gate.creole.gazetteer.DefaultGazetteer.DEF_GAZ_CASE_SENSITIVE_PARAMETER_NAME,
						new Boolean(caseSensitivity));
		params
				.put(
						gate.creole.gazetteer.DefaultGazetteer.DEF_GAZ_ENCODING_PARAMETER_NAME,
						encoding);

		FeatureMap features = Factory.newFeatureMap();
		gate.creole.gazetteer.DefaultGazetteer defaultGazetter = (gate.creole.gazetteer.DefaultGazetteer) Factory
				.createResource("gate.creole.gazetteer.DefaultGazetteer",
						params, features);

		controller.add(defaultGazetter);

	}

}
